<?php

namespace app\controller\web;

use app\exception\ErrnoPlus;
use app\service\ModuleInfoService;
use app\validate\ModuleInfoValidate;
use app\validate\PageValidate;
use share\exception\ApiException;
use share\utils\Helper;

class ModuleInfoController extends WebBaseController{

    public function initValidate(){        
        $this->validate = new ModuleInfoValidate();
    }

    /**
     * 新增模块信息
     */
    public function add_action(){
        $input = $this->check();
        $data = ModuleInfoService::addModule($input);
        return self::ok($data);
    }

    // /**
    //  * 修改模块信息
    //  */
    // public function edit_action(){
    //     $input = $this->check();
    //     $data = ModuleInfoService::updateModule($input);
    //     return self::ok($data);
    // }

    /**
     * 删除模块信息
     */
    public function del_action(){
        $input = $this->check();
        $data = ModuleInfoService::delModule($input['id']);
        return self::ok($data);
    }

    /**
     * 列表 搜索
     */
    public function list_action(){        
        $page = $this->check('pageInfo',null,(new PageValidate()));
        $input = $this->check();       
        $data = ModuleInfoService::getListInfo($input,$page);
        $data = Helper::arrayListReplaceKey($data,[
            'describe_id' => 'describeId',
            'create_time' => 'createTime',
            'update_time' => 'updateTime',
        ]);
        $total = ModuleInfoService::getListCountInfo($input);
        return self::ok(self::formatPageData($page,$total,$data));
    }

    /**
     * 获取指定语言的功能模块
     */
    public function get_by_language_action(){
        $language = $this->request->param('language');
        if(empty($language)){
            throw new ApiException("param language lost",ErrnoPlus::COMMON_ERROR);
        }
        $data =  ModuleInfoService::getAllByLanguage($language);
        return self::ok(Helper::listArrayColumn($data,['id','describeName']));
    }
}